package com.njmaomao.gj.pyot.service;

import com.njmaomao.gj.pyot.common.ResponseCode;
import com.njmaomao.gj.pyot.common.Utils;
import com.njmaomao.gj.pyot.common.exception.SystemBizFail;
import com.njmaomao.gj.pyot.domain.wechat.CallbackResp;
import com.njmaomao.gj.pyot.domain.wechat.PaymentOrder;
import com.njmaomao.gj.pyot.domain.wechat.RefundCallbackMessage;
import com.njmaomao.gj.pyot.domain.model.PayRecord;
import com.njmaomao.gj.pyot.domain.model.RefundRecord;
import com.njmaomao.gj.pyot.service.domain.PaymentOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 */
@Service
public class CallbackSevice {

    private Logger log = LoggerFactory.getLogger(WechatNotificationAppService.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private PaymentOrderService paymentOrderService;

    public void payResultCallback(PayRecord payRecord) {
        switch (PayRecord.OrderType.get(payRecord.getOrderType())) {
            case ORDER:
                if (notifyOrder(payRecord) == false) {
                    log.info("处理订单支付成功失败,放进MQ延迟重试：{}", Utils.toJson(payRecord));
                }
                break;
            case APPDEFINED:
                log.info("自定义订单支付成功,放进MQ通知相关应用：{}", Utils.toJson(payRecord));
                break;
            default:
                SystemBizFail.happen(ResponseCode.PAYTO.UNKNOW_ORDERTYPE);
        }


    }

    private boolean notifyOrder(PayRecord payRecord) {
        /*try {
            PaymentOrder order = paymentOrderService.getOrder(payRecord.getOrderCode(), payRecord.getCustomerId());
            if(order.isPaied()){
                return true;
            }
            return paymentOrderService.updateOrderPaid(payRecord);
        } catch (Exception e) {
            log.error("notifyOrder error, orderCode:" + payRecord.getOrderCode(), e);
        }*/
        PaymentOrder order = paymentOrderService.getOrder(payRecord.getOrderCode(), payRecord.getCustomerId());
        if(order.isPaied()){
            return true;
        }
        return paymentOrderService.updateOrderPaid(payRecord);
    }


    public void refundResultCallback(RefundRecord refundRecord) {
        if (refundRecord.necessaryCallback()) {
            RefundCallbackMessage message = RefundCallbackMessage.newBuilder()
                                                                .withSeqNo(Utils.getSeqNo())
                                                                .withFromSystem("PAYMENT_SYSTEM")
                                                                .withSourceSystem(refundRecord.getFromSystem())
                                                                .withRefundNo(refundRecord.getRefundNo())
                                                                .withRefundId(refundRecord.getRefundId())
                                                                .withRefundAmount(refundRecord.getRefundAmount())
                                                                .withStatus(refundRecord.getStatus())
                                                                .build();
            CallbackResp resp = null;
            try {
                resp = restTemplate.postForObject(refundRecord.getCallbackUrl(), message, CallbackResp.class);
            } catch (Exception e) {
                log.error("refundResultCallback error:" + Utils.toJson(message), e);
            }
            if (resp == null || !resp.isSuccess()) {
                log.warn("通知应用订单已退款失败:{}", Utils.toJson(refundRecord));
            }
        }
    }

}
